﻿#region Copyright and License
/*==============================================================================
 *  Copyright (c) www.51mvc.com Corporation.  All rights reserved.
 * ===============================================================================
 * This code and information is provided "as is" without warranty of any kind,
 * either expressed or implied, including but not limited to the implied warranties
 * of merchantability and fitness for a particular purpose.
 * ===============================================================================
 * Licensed under the GNU General Public License (GPL) v2
 * http://www.51mvc.com/
 * Create By 小鱼(Leven)  - http://leven.com.cn
 * ==============================================================================*/
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Infancy.Core.Container {
    public abstract class ContainerBase : IContainer {
        public ContainerBase(string name) {
            Name = name;
        }

        #region IContainer Members

        public string Name {
            get;
            private set;
        }

        public abstract void Initialize();

        public abstract T Resolve<T>();

        public abstract void TearDown();

        public event EventHandler<ContainerEventsArgs> Initializing;

        public event EventHandler<ContainerEventsArgs> Initialized;

        public event EventHandler<ContainerEventsArgs> TearDowning;

        public event EventHandler<ContainerEventsArgs> TearDowned;

        public event EventHandler<ContainerFailedEventsArgs> ResolveFailed;

        #endregion

        public virtual void OnInitializing(ContainerEventsArgs e) {
            if (Initializing != null) {
                Initializing(this, e);
            }
        }

        public virtual void OnInitialized(ContainerEventsArgs e) {
            if (Initialized != null) {
                Initialized(this, e);
            }
        }

        public virtual void OnTearDowning(ContainerEventsArgs e) {
            if (TearDowning != null) {
                TearDowning(this, e);
            }
        }

        public virtual void OnTearDowed(ContainerEventsArgs e) {
            if (TearDowned != null) {
                TearDowned(this, e);
            }
        }

        public virtual void OnResolveFailed(ContainerFailedEventsArgs e) {
            if (ResolveFailed != null) {
                ResolveFailed(this, e);
            }
        }

        public virtual void Dispose() {
            TearDown();
        }
    }
}
